home *** CD-ROM | disk | FTP | other *** search
/ The Utilities Experience / The Utilities Experience - Volume 1.iso / software / misc / o-z / x-windows / gs262 / gsmisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-29  |  9.7 KB  |  357 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsmisc.c */
  20. /* Miscellaneous utilities for Ghostscript library */
  21. #include "gx.h"
  22. #include "errno_.h"
  23. #include "malloc_.h"
  24. #include "memory_.h"
  25. #ifdef AMIWIN
  26. #include "gdevxamiga.h"
  27. extern void *_XMalloc(size_t);
  28. extern void XFree(void *);
  29. #endif
  30.  
  31. /* Define private replacements for stdin, stdout, and stderr. */
  32. FILE *gs_stdin, *gs_stdout, *gs_stderr;
  33. /* Ghostscript writes debugging output to gs_debug_out. */
  34. /* We define gs_debug and gs_debug_out even if DEBUG isn't defined, */
  35. /* so that we can compile individual modules with DEBUG set. */
  36. #ifdef AMIGA
  37. unsigned
  38. #endif
  39. char gs_debug[128];
  40. FILE *gs_debug_out;
  41. /* We define gs_log_errors here for the same reason. */
  42. int gs_log_errors = 0;
  43.  
  44. /* We can turn on allocator debugging even if DEBUG isn't defined. */
  45. int gs_alloc_debug = 0;
  46. byte gs_alloc_fill_alloc = 0xa1;
  47. byte gs_alloc_fill_free = 0xf1;
  48.  
  49. /* ------ Substitutes for missing C library functions ------ */
  50.  
  51. #ifdef memory__need_memset        /* see memory_.h */
  52. void
  53. memset(void *dest, register char ch, uint len)
  54. {    if ( ch == 0 )
  55.         bzero(dest, len);
  56.     else if ( len > 0 )
  57.     {    register char *p = dest;
  58.         register uint count = len;
  59.         do { *p++ = ch; } while ( --count );
  60.     }
  61. }
  62. #endif
  63.  
  64. #ifdef memory__need_memchr        /* see memory_.h */
  65. const char *
  66. memchr(const char *ptr, char ch, uint len)
  67. {    if ( len > 0 )
  68.     {    register const char *p = ptr;
  69.         register uint count = len;
  70.         do { if ( *p == ch ) return p; p++; } while ( --count );
  71.     }
  72.     return 0;
  73. }
  74. #endif
  75.  
  76. /* ------ Ghostscript utilities ------ */
  77.  
  78. /* Generate a block of unique IDs. */
  79. static ulong gs_next_id = 0;
  80. ulong
  81. gs_next_ids(uint count)
  82. {    ulong id;
  83.     if ( gs_next_id == 0 ) gs_next_id++;
  84.     id = gs_next_id;
  85.     gs_next_id += count;
  86.     return id;
  87. }
  88.  
  89. /*
  90.  * Versions of malloc and free compatible with Ghostscript's
  91.  * model of memory management.  We keep track of all allocated
  92.  * blocks so we can free them at cleanup time.
  93.  */
  94. const gs_memory_procs gs_default_memory_procs = {
  95.     gs_malloc,
  96.     gs_free
  97. };
  98. /* We must make sure that malloc_blocks leave the block aligned. */
  99. typedef struct malloc_block_s malloc_block;
  100. #define malloc_block_data\
  101.     malloc_block *next;\
  102.     uint size;\
  103.     const char *cname
  104. struct malloc_block_data_s { malloc_block_data; };
  105. struct malloc_block_s {
  106.     malloc_block_data;
  107. /* ANSI C does not allow zero-size arrays, so we need the following */
  108. /* unnecessary and wasteful workaround: */
  109. #define _npad (-sizeof(struct malloc_block_data_s) & 7)
  110.     byte _pad[(_npad == 0 ? 8 : _npad)];    /* pad to double */
  111. #undef _npad
  112. };
  113. private malloc_block *malloc_list = 0;
  114. char *
  115. gs_malloc(uint num_elts, uint elt_size, const char *client_name)
  116. {    char *ptr;
  117.     const char *msg = "";
  118.     if ( elt_size != 0 &&
  119.          num_elts > (max_uint - sizeof(malloc_block)) / elt_size
  120.        )
  121.        {    /* Can't represent the size in a uint! */
  122.         msg = "too large for size_t";
  123.         ptr = 0;
  124.        }
  125.     else
  126.        {    uint size = num_elts * elt_size;
  127. #ifndef AMIWIN
  128.         ptr = malloc(size + sizeof(malloc_block));
  129. #else
  130.         ptr = _XMalloc(size + sizeof(malloc_block));
  131. #endif
  132.         if ( ptr == 0 )
  133.             msg = "failed";
  134.         else
  135.            {    malloc_block *bp = (malloc_block *)ptr;
  136.             bp->next = malloc_list;
  137.             bp->size = size;
  138.             bp->cname = client_name;
  139.             malloc_list = bp;
  140.             msg = "OK";
  141.             ptr = (char *)(bp + 1);
  142.             if ( gs_alloc_debug )
  143.               { /* Clear the block in an attempt to track down */
  144.                 /* uninitialized data errors. */
  145.                 memset(ptr, gs_alloc_fill_alloc, size);
  146.               }
  147.            }
  148.        }
  149.     if ( gs_alloc_debug || !*msg )
  150.         dprintf5("gs_malloc(%s)(%u, %u) = 0x%lx: %s\n", client_name,
  151.              num_elts, elt_size, (ulong)ptr, msg);
  152.     return ptr;
  153. }
  154. void
  155. gs_free(char *ptr, uint num_elts, uint elt_size, const char *client_name)
  156. {    malloc_block *bp = malloc_list;
  157.     if ( gs_alloc_debug )
  158.         dprintf4("gs_free(%s)(0x%lx, %u, %u)\n", client_name,
  159.              (ulong)ptr, num_elts, elt_size);
  160.     if ( ptr == (char *)(bp + 1) )
  161.       {
  162. #ifdef DEBUG
  163.         if ( bp->size != num_elts * elt_size )
  164.           lprintf5("%s: free 0x%lx(%u,%u) size ~= %u\n",
  165.                client_name, (ulong)ptr, num_elts, elt_size,
  166.                bp->size);
  167. #endif
  168.         malloc_list = bp->next;
  169.         if ( gs_alloc_debug )
  170.           memset((char *)(bp + 1), gs_alloc_fill_free, bp->size);
  171. #ifndef AMIWIN
  172.         free(bp);
  173. #else
  174.         XFree(bp);
  175. #endif
  176.       }
  177.     else
  178.       { malloc_block *np;
  179.         for ( ; (np = bp->next) != 0; bp = np )
  180.           { if ( ptr == (char *)(np + 1) )
  181.           {
  182. #ifdef DEBUG
  183.             if ( np->size != num_elts * elt_size )
  184.               lprintf5("%s: free 0x%lx(%u,%u) size ~= %u\n",
  185.                    client_name, (ulong)ptr, num_elts, elt_size,
  186.                    np->size);
  187. #endif
  188.             bp->next = np->next;
  189.             if ( gs_alloc_debug )
  190.               memset((char *)(np + 1), gs_alloc_fill_free, np->size);
  191. #ifndef AMIWIN
  192.             free(np);
  193. #else
  194.             XFree(np);
  195. #endif
  196.             return;
  197.           }
  198.           }
  199.         lprintf4("%s: free 0x%lx(%u,%u) not found\n",
  200.              client_name, (ulong)ptr, num_elts, elt_size);
  201. #ifndef AMIWIN
  202.         free((char *)((malloc_block *)ptr - 1));
  203. #else
  204.         XFree((char *)((malloc_block *)ptr - 1));
  205. #endif
  206.       }
  207. }
  208. void
  209. gs_malloc_release(void)
  210. {    malloc_block *bp = malloc_list;
  211.     malloc_block *np;
  212.     for ( ; bp != 0; bp = np )
  213.        {    np = bp->next;
  214.         if ( gs_alloc_debug )
  215.           memset((char *)(bp + 1), gs_alloc_fill_free, bp->size);
  216. #ifndef AMIWIN
  217.         free(bp);
  218. #else
  219.         XFree(bp);
  220. #endif
  221.        }
  222.     malloc_list = 0;
  223. }
  224.  
  225. /* Transpose an 8 x 8 block of bits.  line_size is the raster of */
  226. /* the input data.  dist is the distance between output bytes. */
  227. /* This routine may be supplanted by assembly code. */
  228. #if !USE_ASM
  229.  
  230. #if 1        /* This is the better of the two algorithms. */
  231.  
  232. void
  233. memflip8x8(const byte *inp, int line_size, byte *outp, int dist)
  234. {    register uint ae, bf, cg, dh;
  235.        {    const byte *ptr4 = inp + (line_size << 2);
  236.         ae = ((uint)*inp << 8) + *ptr4;
  237.         inp += line_size, ptr4 += line_size;
  238.         bf = ((uint)*inp << 8) + *ptr4;
  239.         inp += line_size, ptr4 += line_size;
  240.         cg = ((uint)*inp << 8) + *ptr4;
  241.         inp += line_size, ptr4 += line_size;
  242.         dh = ((uint)*inp << 8) + *ptr4;
  243.        }
  244.  
  245.     /* Check for all 8 bytes being the same. */
  246.     /* This is especially worth doing for the case where all are zero. */
  247.     if ( ae == bf && ae == cg && ae == dh && (ae >> 8) == (ae & 0xff) )
  248.        {    if ( ae == 0 ) goto store;
  249.         *outp = -((ae >> 7) & 1);
  250.         outp += dist;
  251.         *outp = -((ae >> 6) & 1);
  252.         outp += dist;
  253.         *outp = -((ae >> 5) & 1);
  254.         outp += dist;
  255.         *outp = -((ae >> 4) & 1);
  256.         outp += dist;
  257.         *outp = -((ae >> 3) & 1);
  258.         outp += dist;
  259.         *outp = -((ae >> 2) & 1);
  260.         outp += dist;
  261.         *outp = -((ae >> 1) & 1);
  262.         outp += dist;
  263.         *outp = -(ae & 1);
  264.         return;
  265.        }
  266.  
  267.        {    register uint temp;
  268.  
  269. /* Transpose a block of bits between registers. */
  270. #define transpose(r,s,mask,shift)\
  271.   r ^= (temp = ((s >> shift) ^ r) & mask);\
  272.   s ^= temp << shift
  273.  
  274. /* Transpose blocks of 4 x 4 */
  275. #define transpose4(r) transpose(r,r,0x00f0,4)
  276.     transpose4(ae);
  277.     transpose4(bf);
  278.     transpose4(cg);
  279.     transpose4(dh);
  280.  
  281. /* Transpose blocks of 2 x 2 */
  282.     transpose(ae, cg, 0x3333, 2);
  283.     transpose(bf, dh, 0x3333, 2);
  284.  
  285. /* Transpose blocks of 1 x 1 */
  286.     transpose(ae, bf, 0x5555, 1);
  287.     transpose(cg, dh, 0x5555, 1);
  288.  
  289.        }
  290.  
  291. store:    *outp = ae >> 8;
  292.     outp += dist;
  293.     *outp = bf >> 8;
  294.     outp += dist;
  295.     *outp = cg >> 8;
  296.     outp += dist;
  297.     *outp = dh >> 8;
  298.     outp += dist;
  299.     *outp = (byte)ae;
  300.     outp += dist;
  301.     *outp = (byte)bf;
  302.     outp += dist;
  303.     *outp = (byte)cg;
  304.     outp += dist;
  305.     *outp = (byte)dh;
  306. }
  307.  
  308. #else        /* This looked like a good idea, but it's no faster. */
  309.  
  310. /* Transpose an 8 x 8 block of bits.  line_size is the raster of */
  311. /* the input data.  dist is the distance between output bytes. */
  312. void
  313. memflip8x8(const byte *inp, int line_size, byte *outp, int dist)
  314. {    /* Define a table that spreads the bits of its index as follows: */
  315.     /* 0->0-3, 1->8-11, 2->16-19, 3->24-27, */
  316.     /* 4->4-7, 5->12-15, 6->20-23, 7->28-31. */
  317. #define b4(v) v,v+0xf,v+0xf00,v+0xf0f
  318. #define b8(v) b4(v),b4(v+0xf0000)
  319. #define b16(v) b8(v),b8(v+0xf000000)
  320.     static const ulong spread[256] =
  321.      { b16(0), b16(0xf0), b16(0xf000), b16(0xf0f0),
  322.        b16(0xf00000), b16(0xf000f0), b16(0xf0f000), b16(0xf0f0f0),
  323.        b16(0xf0000000), b16(0xf00000f0), b16(0xf000f000), b16(0xf000f0f0),
  324.        b16(0xf0f00000), b16(0xf0f000f0), b16(0xf0f0f000), b16(0xf0f0f0f0)
  325.      };
  326.     register ulong hi, lo, temp;
  327.     hi = spread[*inp] & 0x88888888;
  328.     inp += line_size;
  329.     hi |= spread[*inp] & 0x44444444;
  330.     inp += line_size;
  331.     hi |= spread[*inp] & 0x22222222;
  332.     inp += line_size;
  333.     hi |= spread[*inp] & 0x11111111;
  334.     inp += line_size;
  335.     lo = spread[*inp] & 0x88888888;
  336.     inp += line_size;
  337.     lo |= spread[*inp] & 0x44444444;
  338.     inp += line_size;
  339.     lo |= spread[*inp] & 0x22222222;
  340.     inp += line_size;
  341.     lo |= spread[*inp] & 0x11111111;
  342.     temp = (hi & 0xf0f0f0f0) | ((lo >> 4) & 0x0f0f0f0f);
  343.     *outp = (byte)((uint)(temp >> 16) >> 8); outp += dist;
  344.     *outp = (byte)(temp >> 16); outp += dist;
  345.     *outp = (byte)((uint)temp >> 8); outp += dist;
  346.     *outp = (byte)(temp); outp += dist;
  347.     temp = ((hi << 4) & 0xf0f0f0f0) | (lo & 0x0f0f0f0f);
  348.     *outp = (byte)((uint)(temp >> 16) >> 8); outp += dist;
  349.     *outp = (byte)(temp >> 16); outp += dist;
  350.     *outp = (byte)((uint)temp >> 8); outp += dist;
  351.     *outp = (byte)(temp);
  352. }
  353.  
  354. #endif        /* memflip8x8 */
  355.  
  356. #endif                    /* !USE_ASM */
  357.